package com.wg.springboot.jdk.time;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

import static java.time.temporal.ChronoUnit.MINUTES;

public class DateTime {
    public static void main(String[] args) {
        /*
        java.time.LocalDate;
        java.time.LocalDateTime;
        java.time.LocalTime;
        */
//        LocalDate
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate); //2021-10-09
        LocalDate localDate1 = LocalDate.of(2021, 1, 1);
        System.out.println(localDate1);//2021-01-01
        int year = localDate.getYear();
        int monthValue = localDate.getMonthValue();
        System.out.println(year);
//        取到月对应的枚举值
        Month month = localDate.getMonth();
        System.out.println(month);  //OCTOBER
        int value = month.getValue();//反过来从枚举值中取具体月份
        System.out.println(value); //10
//        该月的最大天数
        int i = month.maxLength();
        System.out.println(i);
//       按月取该月当前几号
        int dayOfMonth = localDate.getDayOfMonth();
        System.out.println(dayOfMonth);
//        今年总共过去多少天
        int dayOfYear = localDate.getDayOfYear();
        System.out.println(dayOfYear);
//        今天是周几的枚举值
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        System.out.println(dayOfWeek);
//        今天是周几
        int value1 = dayOfWeek.getValue();
        System.out.println(value1);
//        LocalDateTime & LocalTime
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime); //2021-10-09T17:21:36.767147100
        LocalDateTime localDateTime2 = LocalDateTime.of(2020, 2, 2, 13, 24, 52);
        System.out.println(localDateTime2);

        LocalTime now = LocalTime.now();
        System.out.println(now);  //17:23:08.721161700

        System.out.println(localDateTime.getHour());//时
        System.out.println(localDateTime2.getHour());//时
        System.out.println(now.getHour());//时
        System.out.println(localDateTime2.getMinute());//分
        System.out.println(localDateTime2.getSecond());//秒
//        调整时间底层用的是while循环case相等后return的是传入的新的
        LocalDateTime with = localDateTime.with(localDateTime2);
        System.out.println(with);
        System.out.println("localDateTime是：" + localDateTime);
        LocalDateTime plus = localDateTime.plus(1, MINUTES);  //加一分钟
        System.out.println(plus);
        LocalDateTime minus = plus.minus(1, MINUTES);
        System.out.println(minus);


        LocalTime localTime = LocalTime.now();
        LocalDate localDate2 = LocalDate.now();
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println(localTime);  //11:14:25.033938
        System.out.println(localDate2);  //2021-10-11
        System.out.println(localDateTime1); //2021-10-11T11:14:25.033938
//        另一个时间类精确到纳秒
        Instant now1 = Instant.now();
        System.out.println(now1);
//        添加偏移量8小时  就是当前时间了
        OffsetDateTime offsetDateTime = now1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
//        获取总共多少毫秒  该方法属于Instant类
        long l = now1.toEpochMilli();
        System.out.println(l);
//        调用静态方法传入秒数得到具体时间  官方言辞叫做时间戳注入产生实例instant对象
        Instant instant = Instant.ofEpochMilli(1);
        System.out.println(instant);
//        转换对象  预定义标准格式
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
        System.out.println(isoDateTime);
//        时间对象
        LocalDateTime localDateTime3 = LocalDateTime.now();
//        传入到转换对象里面去转换
        String format = isoDateTime.format(localDateTime3);
        System.out.println(format);  //2021-10-11T11:34:34.4945392
        System.out.println(format);  //2021-10-11T11:34:34.4945392
        TemporalAccessor parse = isoDateTime.parse("2020-03-19T22:09:46.345");
        System.out.println(parse);
        /*
        // ofPattern("自定义格式"); "yyyy-MM-dd hh:mm:ss"
        // FormatStyle.FULL     2020年4月19日 星期日
        // FormatStyle.LONG     2020年4月19日 下午10时15分01秒
        // FormatStyle.MEDIUM   2020-4-19 22:14:28
        // FormatStyle.SHORT    20-4-19 下午10:16
         */
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format1 = dateTimeFormatter.format(localDateTime3);
        System.out.println(format1); //2021年10月11日星期一
    }
}
